/*
 * Created on May 27, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.novix.portal.framework.dao.impl;

import java.util.ArrayList;
import java.util.List;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;

import org.apache.log4j.Logger;

import com.novix.portal.framework.dao.IWebOrganizationDAO;
import com.novix.portal.framework.exceptions.ApplicationException;
import com.novix.portal.framework.model.impl.WebOrganization;

/**
 * @author Dev
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class WebOrganizationDAO extends BaseDAO implements IWebOrganizationDAO {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(WebOrganizationDAO.class);

    private static final boolean DEBUG_ENABLED = logger.isDebugEnabled();

    private static final boolean IS_DEBUG = DEBUG_ENABLED;

    /**
     *  
     */
    public WebOrganizationDAO() {
        super();
      
    }
    /**
     *  called from cache loader
     */

    public List selectAllWebOrganizations(Session session, String serverName, String ienaddressesPath) {
        List list = null;
        try {
            Database notesDatabase = session.getDatabase(serverName, ienaddressesPath);
            if (!notesDatabase.isOpen())
                notesDatabase.open();
            View view = notesDatabase.getView("WebMemberCo");            

            Document doc =view.getFirstDocument();
            Document temp = null; 
            list = new ArrayList();
            while (doc != null) {
                WebOrganization webOrg = new WebOrganization();
                webOrg.setCompanyName(doc.getItemValueString("CompanyName"));
                setRegionLinks(doc, webOrg);
                webOrg.setOfficeCity(doc.getItemValueString("OfficeCity"));
                webOrg.setOfficePhoneNumber(doc.getItemValueString("OfficePhoneNumber"));
                webOrg.setOfficeState(doc.getItemValueString("OfficeState"));
                webOrg.setOrgComment(doc.getItemValueString("OrgComment"));            
                webOrg.setNAICS(getNaics(doc));
                webOrg.setCompanyDocumentUniqueID(doc.getItemValueString("IENCompanyDocumentUniqueID"));
                webOrg.setCompanyId(doc.getItemValueString("IENCompanyDocumentUniqueID"));
                webOrg.setCaptives(doc.getItemValue("captives"));
                list.add(webOrg);
//                if (IS_DEBUG)
//                    logger.debug(doc.getUniversalID());
                temp = view.getNextDocument(doc); // get the next one
                doc.recycle(); // recycle the one we're done with
                doc = temp;
            }

        } catch (NotesException e) {
            String errorMessage = "Error in selectAllWebOrganizations ...";
            logger.error(errorMessage + " " + e.id + " " + e.text, e);
            throw new ApplicationException(errorMessage);
        }
        return list;

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.novix.portal.framework.dao.impl.BaseDAO#selectAllWebOrganizations(java.lang.String,
     *      java.lang.String, java.lang.String, java.lang.String,
     *      java.lang.String)
     */
    public List selectAllWebOrganizations(String serverConnectionString, String token, String captiveName, String serverName, String ienaddressesPath) {
     
        return super.selectAllWebOrganizations(serverConnectionString, token, captiveName, serverName, ienaddressesPath);
    }

    /* (non-Javadoc)
     * @see com.novix.portal.framework.dao.IWebOrganizationDAO#selectAllWebOrganizationsByQuery(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public List selectAllWebOrganizationsByQuery(String query, String serverConnectionString, String token, String captiveName, String serverName, String ienaddressesPath) {
        return super.selectAllWebOrganizationsByQuery(query, serverConnectionString, token, captiveName, serverName, ienaddressesPath);
    }

}
